package Queuedemo;

public class MyLinkQueue {
    static class LinkNode {
        int val;
        LinkNode next;
        LinkNode prev;
        public LinkNode(int val) {
            this.val = val;
        }
    }
    private LinkNode head;
    private LinkNode tail;
    public int usedSize;

    public boolean offer(int val) {
        LinkNode newNode = new LinkNode(val);
        if(head==null){
            head = newNode;
            tail = newNode;
        }else{
            tail.next=newNode;
            newNode.prev=tail;
            tail = newNode;
        }
        usedSize++;
        return true;
    }

    public int poll(){
        if(head==null){
            return -1;
        }
        int val = head.val;
        if(head.next==null){
            head=null;
            tail=null;
            return val;
        }
        head=head.next;
        head.prev=null;
        usedSize--;
        return val;
    }


    public int peek(){
        if(head==null){
            return -1;
        }
        return head.val;
    }

    public boolean isEmpty(){
        return head==null;
    }

    public int size(){
        return usedSize;
    }
}
